﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.world {
    import flash.events.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.text.*;
    import net.wooga.pets.events.*;
    import __AS3__.vec.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.models.world.vos.actors.*;
    import net.wooga.pets.views.*;
    import net.wooga.pets.constants.*;
    import net.wooga.pets.utils.*;
    import net.wooga.pets.models.world.*;

    public class AbstractActorMediator extends AbstractViewMediator implements ITicking {

		[Inject]
        public var worldModel:WorldModel;
		[Inject]
        public var languageModel:LanguageModel;
		[Inject]
        public var tickModel:TickModel;
		[Inject]
        public var gameStateModel:GameStateModel;
        private var _targetPoint:Point;
        private var _targetRoute:Vector.<Point>;
        protected var _vo:IActing;

        public function AbstractActorMediator(){
            this._targetRoute = new Vector.<Point>();
            super();
        }
        override public function onRegister():void{
            this._vo = this.worldModel.getActor(id);
            super.onRegister();
            eventMap.mapListener(eventDispatcher, EventConstants.MOVE_TO_POSITION, this.onMoveToPosition);
            eventMap.mapListener(eventDispatcher, EventConstants.SHOW_SPEECH_BUBBLE, this.onShowSpeechBubble);
            eventMap.mapListener(eventDispatcher, EventConstants.REMOVE_SPEECH_BUBBLE, this.onRemoveSpeechBubble);
            eventMap.mapListener(eventDispatcher, EventConstants.SET_SPEECHBUBBLE_VISIBLE, this.onSetSpeechbubbleVisible);
            eventMap.mapListener(eventDispatcher, EventConstants.SET_ANIMATION_STATE, this.onSetAnimationState);
            eventMap.mapListener(eventDispatcher, EventConstants.HIGHLIGHT_ACTOR, this.onHighlight);
            eventMap.mapListener(eventDispatcher, EventConstants.ON_HOSPITAL_LEFT, this.onRemoveActor);
            eventMap.mapListener(eventDispatcher, EventConstants.ON_REMVED_FROM_WAITING_QUEUE, this.onRemoveActor);
            eventMap.mapListener(this.view, MouseEvent.CLICK, this.onClick);
            eventMap.mapListener(this.view, MouseEvent.MOUSE_OVER, this.onMouseOver);
            eventMap.mapListener(this.view, MouseEvent.MOUSE_OUT, this.onMouseOut);
            eventMap.mapListener(eventDispatcher, EventConstants.CLICK_ON_SPEECHBUBBLE, this.onClickOnSpeechbubble);
            eventMap.mapListener(eventDispatcher, EventConstants.REMOVE_MOUSE_OVER_EFFECT, this.onRemoveMouseOver);
            eventMap.mapListener(eventDispatcher, EventConstants.FADE_ACTOR, this.onFadeActor);
            eventMap.mapListener(eventDispatcher, EventConstants.UPDATE_ACTOR, this.onUpdateActor);
            eventMap.mapListener(eventDispatcher, EventConstants.TURN_LEFT, this.onTurnActor);
            eventMap.mapListener(eventDispatcher, EventConstants.TURN_RIGHT, this.onTurnActor);
            eventMap.mapListener(eventDispatcher, BuildingEvent.FACADE_CHANGED, this.toggleFacade);
            addContextListener(EventConstants.START_MOVING, this.onStartMoving);
            addContextListener(EventConstants.STOP_MOVING, this.onStopMoving);
            this.view.setState(this.vo.state);
            this.view.setAnimationState(this.vo.animationState, true);
            dispatch(new InfoEvent(EventConstants.SHOW_SPEECH_BUBBLE, id));
        }
        override protected function storeViewRectangle():void{
            AbstractActorVO(this._vo).viewRectangle = this.view.rectangle;
        }
        private function onStartMoving(_arg1:InfoEvent):void{
            if (_arg1.id == id){
                this.startMoving();
            };
        }
        private function onStopMoving(_arg1:InfoEvent):void{
            if (_arg1.id == id){
                this.stopMoving();
            };
        }
        private function onRemoveActor(_arg1:InfoEvent):void{
            if (_arg1.id == id){
                Displays.removeChild(this.view);
                mediatorMap.removeMediator(this);
            };
        }
        protected function onFadeActor(_arg1:InfoEvent):void{
            var _local2:Object;
            if (_arg1.id == id){
                _local2 = _arg1.info;
                if (_local2.enabled){
                    this.view.setEnabled();
                } else {
                    this.view.setDisabled();
                };
                this.view.alpha = _local2.alpha;
                this.view.visible = ((this.vo.isVisible) && ((_local2.alpha > 0)));
            };
        }
        private function onTurnActor(_arg1:InfoEvent):void{
            if (_arg1.id == id){
                this.view.direction = ((_arg1.type)==EventConstants.TURN_LEFT) ? 1 : -1;
            };
        }
        private function onUpdateActor(_arg1:InfoEvent):void{
            if (_arg1.id == id){
                this.updateActor(_arg1.info);
            };
        }
        protected function updateActor(_arg1:Object):void{
            _arg1 = ((_arg1) || ({}));
            this.view.setState(this.vo.animationState);
            this.view.setAnimationState(_arg1.animationState, true);
        }
        private function get vo():IActing{
            return (this._vo);
        }
        private function get view():AbstractActorView{
            return ((viewComponent as AbstractActorView));
        }
        private function onShowSpeechBubble(_arg1:InfoEvent):void{
            if ((((((id == _arg1.id)) && ((this._vo is ISpeaking)))) && (!(userModel.isVisitFriendsMode())))){
                switch ((this._vo as ISpeaking).speechBubbleType){
                    case SpeechBubbleConstants.TYPE_ICON_BUBBLE:
                        this.setIconBubble();
                        break;
                    case SpeechBubbleConstants.TYPE_TEXT_BUBBLE:
                        this.setTextBubble();
                        break;
                    case SpeechBubbleConstants.TYPE_CUSTOM_BUBBLE:
                        this.setCustomBubble();
                        break;
                    case SpeechBubbleConstants.TYPE_STORY_BUBBLE:
                        this.setStoryBubble();
                        break;
                    case SpeechBubbleConstants.TYPE_ENERGY_BUBBLE:
                        this.setEnergyBubble();
                        break;
                    case SpeechBubbleConstants.TYPE_BOOST_BUBBLE:
                        this.setBoostBubble();
                        break;
                };
            };
        }
        private function onRemoveSpeechBubble(_arg1:InfoEvent):void{
            if (id == _arg1.id){
                this.view.removeSpeechBubble();
            };
        }
        private function onSetSpeechbubbleVisible(_arg1:InfoEvent):void{
            if ((((id == _arg1.id)) && (this.view.speechBubbleContainer))){
                this.view.speechBubbleContainer.visible = (_arg1.info as Boolean);
            };
        }
        private function onSetAnimationState(_arg1:InfoEvent):void{
            if (id == _arg1.id){
                this.view.setAnimationState(_arg1.info.toString());
            };
        }
        private function onHighlight(_arg1:InfoEvent):void{
            if (id == _arg1.id){
                this.handleHighlight();
            };
        }
        protected function handleHighlight():void{
            this.view.highlightEffect();
            dispatch(new InfoEvent(EventConstants.HIGHLIGHT_ROOM, this.vo.roomId));
        }
        protected function onMoveToPosition(_arg1:InfoEvent):void{
            if (_arg1.id == id){
                this.setTargetRoute(Vector.<Point>(_arg1.info));
            };
        }
        protected function setTargetRoute(_arg1:Vector.<Point>):void{
            this._targetRoute = _arg1;
            this._targetPoint = this._targetRoute.shift();
            this.startMoving();
        }
        protected function startMoving():void{
            if (this._targetPoint){
                this.vo.isMoving = true;
                dispatch(new TickingEvent(TickingEvent.ADD_TICKING, this));
            };
        }
        override public function preRemove():void{
            this.stopMoving();
            super.preRemove();
        }
        protected function stopMoving():void{
            this.vo.isMoving = false;
            dispatch(new TickingEvent(TickingEvent.REMOVE_TICKING, this));
            this.view.setAnimationState(GameConstants.ANIMATION_STATE_STILL);
        }
        protected function setIconBubble():void{
            var _local3:MovieClip;
            var _local4:TextFormat;
            var _local5:TextField;
            var _local1:ISpeaking = (this._vo as ISpeaking);
            var _local2:Object = _local1.speechBubbleData;
            if (_local2.hasOwnProperty("icon")){
                _local3 = AssetsLibrary.getMovieClip(_local2["icon"]);
                this.view.speechBubblePosition = (_local2.position) ? new Point(_local2.position.x, _local2.position.y) : _local1.speechBubblePosition;
                if (_local2.label){
                    _local4 = new TextFormat();
                    _local4.font = "MyriadProFont";
                    _local4.align = TextFormatAlign.CENTER;
                    _local4.bold = true;
                    _local4.color = "0xffffff";
                    _local4.size = 13;
                    _local5 = (Displays.getChild(_local3, _local2.label) as TextField);
                    _local5.embedFonts = true;
                    _local5.htmlText = this.languageModel.getTextByKey(_local2.key);
                    _local5.autoSize = TextFieldAutoSize.CENTER;
                    _local5.multiline = false;
                    _local5.setTextFormat(_local4);
                };
                if (_local3){
                    this.view.addSpeechBubble(_local3);
                };
            };
        }
        protected function setTextBubble():void{
        }
        protected function setCustomBubble():void{
        }
        protected function setStoryBubble():void{
        }
        protected function setEnergyBubble():void{
        }
        protected function setBoostBubble():void{
        }
        protected function onClick(_arg1:MouseEvent):void{
        }
        protected function onClickOnSpeechbubble(_arg1:InfoEvent):void{
            if (_arg1.id == id){
                this.onClick((_arg1.info as MouseEvent));
            };
        }
        protected function onMouseOut(_arg1:MouseEvent):void{
            this.view.mouseOutEffect();
        }
        protected function onRemoveMouseOver(_arg1:InfoEvent):void{
            if (_arg1.id == id){
                this.view.mouseOutEffect();
            };
        }
        protected function onMouseOver(_arg1:MouseEvent):void{
            this.view.mouseOverEffect();
        }
        protected function toggleFacade(_arg1:BuildingEvent=null):void{
        }
        public function onTick(_arg1:Boolean=true, _arg2:Number=0):void{
            var _local6:Number;
            var _local7:Number;
            var _local10:Number;
            var _local11:Number;
            var _local12:Number;
            if (!this._targetPoint){
                this._targetPoint = new Point(this.view.x, this.view.y);
            };
            var _local3:Number = (this._targetPoint.x - this.view.x);
            var _local4:Number = (this._targetPoint.y - this.view.y);
            var _local5:Number = Math.sqrt(((_local3 * _local3) + (_local4 * _local4)));
            var _local8:IMoving = (this._vo as IMoving);
            var _local9:Number = (_local8.speed * this.tickModel.factor);
            if (_local5 > _local9){
                _local10 = Math.atan2(_local4, _local3);
                _local11 = (Math.cos(_local10) * _local9);
                _local12 = (Math.sin(_local10) * _local9);
                _local6 = (this.view.x + _local11);
                _local7 = (this.view.y + _local12);
                setPosition(_local6, _local7);
                if ((((this._targetRoute.length > 0)) && ((this._targetRoute.length < 2)))){
                    this.view.setAnimationState(GameConstants.ANIMATION_STATE_ELEVATOR);
                    this.view.setDisabled();
                } else {
                    this.view.setAnimationState(GameConstants.ANIMATION_STATE_WALKING);
                    if (!this.view.mouseChildren){
                        this.view.setEnabled();
                    };
                };
            } else {
                _local6 = this._targetPoint.x;
                _local7 = this._targetPoint.y;
                setPosition(_local6, _local7);
                if (this._targetRoute.length > 0){
                    this._targetPoint = this._targetRoute.shift();
                } else {
                    this._targetPoint = null;
                    this.stopMoving();
                    dispatch(new InfoEvent(EventConstants.ON_MOVE_TO_POSITION_FINISHED, id));
                };
            };
        }

    }
}//package net.wooga.pets.views.world 
